<?php

namespace app\agent\controller;

use app\common\model\CityAgent;
use app\common\model\User;
use app\common\controller\Agent;
use think\exception\ValidateException;
use think\facade\Db;
use think\Exception;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Order extends Agent
{
    protected $relationSearch = true;

    protected $searchFields = 'id,user.username,worker.username,servicebasic.name,hospital.name';
    
    /**
     * Order模型对象
     * @var \app\common\model\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Order;
        $this->view->assign("medicineTypeList", $this->model->getmedicineTypeList());
        $this->view->assign("selfCareAbilityList", $this->model->getSelfCareAbilityList());
        $this->view->assign("refundStatusList", $this->model->getRefundStatusList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/agent/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            $agent=$this->auth->getUserInfo();
//
//            $agent_city=CityAgent::where(['user_id'=>$agent,'status'=>'normal'])->column();

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->withJoin(['user','hospital','servicebasic','department','patient','worker'])
                    ->where($where)
                    //->where('agent_id','=',$agent['id'])
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->withJoin(['user','hospital','servicebasic','department','patient','worker'])
                    ->where($where)
                    //->where('agent_id','=',$agent['id'])
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                /*$row->visible(['id','user_id','patient_id','hospital_id','begin_time','create_time','finish_time','status','coupon_money','total_price','pay_price']);
                $row->getRelation('user')->visible(['username']);
				$row->getRelation('hospital')->visible(['name']);
				$row->getRelation('servicebasic')->visible(['name']);
				$row->getRelation('hospitaldepartment')->visible(['name']);*/

                $row->visible([
                    'id','user_id','patient_id','hospital_id','begin_time','create_time','finish_time','status','coupon_money','total_price','pay_price',
                    'user'=>['username'],
                    'worker'=>['username'],
                    'hospital'=>['name'],
                    'department'=>['name'],
                    'patient'=>['name'],
                    'servicebasic'=>['code','name'],
                ])->toArray();
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $this->error('不可更新信息');
        }
        $this->view->assign('row', $row);

        return $this->view->fetch();
    }
}
